package mx.itesm.ccm.compu.gui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.TabSheet.SelectedTabChangeListener;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;


public class MainPanel extends Panel {

	Logger log = LoggerFactory.getLogger(MainPanel.class);
	public MainPanel(Rol rol){
		
		super();
		
		//liga para salir
		Button b = new Button("Salir", new ClickListener() {
			
			public void buttonClick(ClickEvent event) {
				event.getButton().getApplication().close();				
			}
		});

		b.setStyleName(BaseTheme.BUTTON_LINK);
		b.setImmediate(true);
		
		//tab principal
		TabSheet tab = new TabSheet();
		tab.setImmediate(true);
		
		Panel inicio = new Panel();
		Label l = new Label("Bienvenido al sistema de reserva de asesorias.");
		inicio.addComponent(l);
		log.debug("Agregada la etiqueta");
		
		tab.addTab(inicio, "Inicio", null);
		log.debug("Se agrega la tab de inicio");
		if(rol == Rol.ALUMNO){
			log.debug("ES un alumno");
			final PanelAlumno al = new PanelAlumno();

			tab.addTab(al, "Reserva", null);
			
			tab.addListener(new SelectedTabChangeListener() {
				
				public void selectedTabChange(SelectedTabChangeEvent event) {
					if(event.getTabSheet().getSelectedTab().equals(al)){
						//al.reinit(); //TODO, agregar este metodo
					}
					
				}
			});
			
			
			
		}
		if(rol== Rol.PROFESOR){
			log.debug("Es un profesor");
			final PanelProfesor prof= new PanelProfesor();
			tab.addTab(prof, "Registra Horario", null);
			
			tab.addListener(new SelectedTabChangeListener() {
				
				public void selectedTabChange(SelectedTabChangeEvent event) {
					if(event.getTabSheet().getSelectedTab().equals(prof)){
						prof.reinit(); 
					}
					
				}
			});
		}
		
		
		//layout
		VerticalLayout layout = new VerticalLayout();
		layout.setSpacing(true);
		layout.setImmediate(true);
		
		//agregar boton
		layout.addComponent(b);
		layout.setWidth(100, VerticalLayout.UNITS_PERCENTAGE);
		layout.setComponentAlignment(b, Alignment.MIDDLE_RIGHT);
		
		//agregar tabs
		layout.addComponent(tab);
		log.debug("Se agrego el tab");
		
		this.addComponent(layout);
		log.debug("Se agrega el layout");
		
	
	}
	
	public enum Rol{
		PROFESOR, ALUMNO
	}
	
}
